﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace TM.QWeibo.Client
{
    public partial class Search : PhoneApplicationPage
    {
        public Search()
        {
            InitializeComponent();
            this.ApplicationBar.BackgroundColor = (Color)App.Current.Resources["ApplicationBarBackgroundColor"];
            searchUsers.Selected=(user)=>{
                if (user != null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri(string.Format("/Profile.xaml?{0}={1}", AppSetting.UserNameParameterKey, user.Name), UriKind.Relative)));
                }
            };
            searchTweet.Selected = (t) =>
                                       {
                                           if (t != null)
                                           {
                                               Deployment.Current.Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri(string.Format("/Tweet/Detail.xaml?{0}={1}", AppSetting.TweetIdParameterKey, t.Id), UriKind.Relative)));
                                           }
                                       };
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            txtKeyword.Focus();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            var index = searchPivot.SelectedIndex;
            tbUserNum.Text = "";
            tbTweetNum.Text = "";
            string keyword = txtKeyword.Text;
            if (string.IsNullOrWhiteSpace(keyword)) {
                txtKeyword.Focus();
                return;
            }
            if (index == 0) {
                searchUsers.Search(keyword, () =>
                {
                    progressBar.IsIndeterminate = true;
                }, (rs) => {
                    progressBar.IsIndeterminate = false;
                    var d = (SDK.Objects.ResponseDataObject<SDK.Objects.UserInfo>)rs;
                    /*if (d==null ||d.Info==null) {
                        (new Helper.MessageBoxHelper()).Show("没有相关的用户，请重试!","提示",this.LayoutRoot);
                    }*/
                    if (d != null) {
                        tbUserNum.Text = AppSetting.CheckBigNumber(d.TotalCount);
                    }
                });
            }
            else if(index==1){
                searchTweet.Search(keyword, () =>
                {
                    progressBar.IsIndeterminate = true;
                }, (rs) => {
                    progressBar.IsIndeterminate = false;
                    var d = (SDK.Objects.ResponseDataObject<SDK.Objects.Tweet>)rs;
                    /*if (d==null ||d.Info==null) {
                        (new Helper.MessageBoxHelper()).Show("没有相关的微博，请重试!","提示",this.LayoutRoot);
                    }*/
                    if (d != null) {
                        tbTweetNum.Text = AppSetting.CheckBigNumber(d.TotalCount);
                    }
                });
            }
        }

        private void txtKeyword_GotFocus(object sender, RoutedEventArgs e)
        {
            txtKeyword.Background = new SolidColorBrush(Color.FromArgb(255, 239, 235, 239));
            txtKeyword.BorderThickness = new Thickness(1);
            txtKeyword.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148));
        }

        private void txtKeyword_LostFocus(object sender, RoutedEventArgs e)
        {
            txtKeyword.Background = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239));
            txtKeyword.BorderThickness = new Thickness(1);
            txtKeyword.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148));
        }

        private void txtKeyword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) {
                this.Focus();
                btnSearch_Click(null,new RoutedEventArgs());
            }
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
            else
            {
                NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative));
            }
        }
    }
}